package adfc2.actions;

import adfc2.core.Creature;
import adfc2.core.Item;
import adfc2.core.Material;
import adfc2.core.World;
import adfc2.entities.DroppedItem;
import adfc2.items.Pickaxe;
import adfc2.items.Stone;

import static adfc2.core.World.world;

public class DigAction extends LongAction {

    public static final int DIG_DURATION = 1000;
    public final int tgtxy;

    public DigAction(Creature actor, int tgtxy) {
        super(actor);
        this.tgtxy = tgtxy;
    }

    private static final Pickaxe PICKAXE = new Pickaxe(Material.IRON);

    @Override
    protected void started() {
        assert World.near(actor.getXY(), tgtxy);
        assert (World.world().blocks[tgtxy] & World.MASK_FILLED) != 0;
        assert actor.hasItemLike(PICKAXE, Item.ICMASK_TYPE);
    }

    @Override
    protected boolean progressing(long dt) {
        return (totaltime >= DIG_DURATION);
    }

    @Override
    protected void finished() {
        world().remBlock(tgtxy);
        new DroppedItem(new Stone(Material.get(world().blocks[tgtxy] & World.MASK_MATERIAL))).register(tgtxy);
    }

    @Override
    protected void interrupted() {
    }

    @Override
    public double estimateProgress() {
        return ((double) totaltime) / DIG_DURATION;
    }
}
